package com.junlong0716.enjoylearning.ui

import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.StudyNoteEntity
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.module.common.base.BasePresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import org.json.JSONObject

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 下午10:04 2018/5/8
 *@modified by:
 */
class StudyRecordPresenter : BasePresenter<StudyRecordContract.View>(), StudyRecordContract.Presenter {
    override fun requestStudyNote(noteActivity: NoteActivity, currentPage: Int, onePageCount: Int) {
        val map = HashMap<String, Int>()
        map["userId"] = MyApp.instance().userAccount!!.id
        map["currentPage"] = currentPage
        map["onePageCount"] = onePageCount
        val jsonObject = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_QUERY_NOTE)
                .tag(noteActivity)
                .upJson(jsonObject)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObject = JSONObject(response!!.body())
                        val jsonArray = jsonObject.getJSONObject("data").getJSONArray("list")
                        val noteData = ArrayList<StudyNoteEntity>()
                        for (i in 0 until jsonArray.length()) {
                            val id = jsonArray.getJSONObject(i).getInt("id")


                            val knowledgeId: Int = try {
                                jsonArray.getJSONObject(i).getInt("knowledgeId")
                            } catch (e: Exception) {
                                0
                            }

                            val noteContent = jsonArray.getJSONObject(i).getString("noteContent")
                            var createDate: Long = try {
                                jsonArray.getJSONObject(i).getLong("createDate")
                            } catch (e: Exception) {
                                0
                            }

                            val remarks = jsonArray.getJSONObject(i).getString("remarks")
                            val knowledgeTitle = jsonArray.getJSONObject(i).getString("knowledgeTitle")

                            //val id = jsonArray.getJSONObject(i).getInt("id")
                            noteData.add(StudyNoteEntity(id, knowledgeId, noteContent, createDate, remarks, knowledgeTitle))
                        }
                        getView()!!.requestStudyNoteSucceed(noteData)
                    }
                })
    }

    override fun requestStudyNote(studyRecordFragment: StudyRecordFragment, currentPage: Int, onePageCount: Int) {
        val map = HashMap<String, Int>()
        map["userId"] = MyApp.instance().userAccount!!.id
        map["currentPage"] = currentPage
        map["onePageCount"] = onePageCount
        val jsonObject = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_QUERY_NOTE)
                .tag(studyRecordFragment)
                .upJson(jsonObject)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObject = JSONObject(response!!.body())
                        val jsonArray = jsonObject.getJSONObject("data").getJSONArray("list")
                        val noteData = ArrayList<StudyNoteEntity>()
                        for (i in 0 until jsonArray.length()) {
                            val id = jsonArray.getJSONObject(i).getInt("id")


                            val knowledgeId: Int = try {
                                jsonArray.getJSONObject(i).getInt("knowledgeId")
                            } catch (e: Exception) {
                                0
                            }

                            val noteContent = jsonArray.getJSONObject(i).getString("noteContent")
                            var createDate: Long = try {
                                jsonArray.getJSONObject(i).getLong("createDate")
                            } catch (e: Exception) {
                                0
                            }

                            val remarks = jsonArray.getJSONObject(i).getString("remarks")
                            val knowledgeTitle = jsonArray.getJSONObject(i).getString("knowledgeTitle")

                            //val id = jsonArray.getJSONObject(i).getInt("id")
                            noteData.add(StudyNoteEntity(id, knowledgeId, noteContent, createDate, remarks, knowledgeTitle))
                        }
                        getView()!!.requestStudyNoteSucceed(noteData)
                    }
                })
    }

    override fun requestQueryMyStudyTime(studyRecordFragment: StudyRecordFragment) {
        val map = HashMap<String, Int>()
        map["userId"] = MyApp.instance().userAccount!!.id
        val jsonObject = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_QUERY_MY_STUDY_TIME)
                .tag(studyRecordFragment)
                .upJson(jsonObject)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getString("code")
                        if (code == "200") {
                            val d1 = jsonObj.getJSONObject("data").getString("d1")
                            val d2 = jsonObj.getJSONObject("data").getString("d2")
                            val d3 = jsonObj.getJSONObject("data").getString("d3")
                            val d4 = jsonObj.getJSONObject("data").getString("d4")
                            val d5 = jsonObj.getJSONObject("data").getString("d5")
                            val d6 = jsonObj.getJSONObject("data").getString("d6")
                            val d7 = jsonObj.getJSONObject("data").getString("d7")
                            val totalTime = jsonObj.getJSONObject("data").getDouble("countTime")

                            val dayList = ArrayList<String>()
                            dayList.add(d1)
                            dayList.add(d2)
                            dayList.add(d3)
                            dayList.add(d4)
                            dayList.add(d5)
                            dayList.add(d6)
                            dayList.add(d7)
                            getView()!!.requestQueryMyStudyTimeSucceed(dayList, totalTime.toString())

                        } else {
                            ToastUtils.showShort("获取统计时间失败！")
                        }
                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                        ToastUtils.showShort("获取统计时间失败！")
                    }
                })
    }

    override fun onDestroy() {

    }

}